const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    to_ship_list: [], //待发货
    shipped_list: [], //已发货
    shop_data: {},
    base_url: "https://vip.join.ink/",
    total_merchants: '',
    yesterday_merchants: '',
    today_merchants: '',
    can_cash: '0',
    sales_detail_query: [],
    active: 1
  },
  // 发货
  to_ship(e) {
    let that = this
    let order_id = e.currentTarget.dataset.order_id
    console.log(order_id);
    wx.scanCode({
      success(res) {  
        console.log(res.result, "单号")
        app.request("/user/add_logistics_number", {
          method: "POST",
          is_shop: true,
          data: {
            logistics_no: res.result,
            orderno: order_id
          }
        }).then(res => {
          console.log(res, "发货回调");
          that.onShow()
        })
      },
      fail(err) {
        console.log(err);
        wx.showToast({
          title: "识别失败",
          icon: 'none'
        })
      }
    })
  },




  fetch() {

    // 待发货
    app.request("/user/store_delivery_list", {
      is_shop: true,
      data: {
        type: 1
      }
    }).then(res => {
      console.log(res, "待发货");
      this.setData({
        to_ship_list: res.data.data
      })
    })

    //已发货
    app.request("/user/store_delivery_list", {
      is_shop: true,
      data: {
        type: 2
      }
    }).then(res => {
      console.log(res, "已发货");
      this.setData({
        shipped_list: res.data.data
      })
    })



    // 总收益
    app.request("/user/total_merchants", {
      is_shop: true
    }).then(res => {
      console.log(res);
      this.setData({
        total_merchants: res.data.data
      })
    })
    // 昨日收益
    app.request("/user/yesterday_merchants", {
      is_shop: true
    }).then(res => {
      console.log(res);
      this.setData({
        yesterday_merchants: res.data.data
      })
    })
    // 今日收益
    app.request("/user/today_merchants", {
      is_shop: true
    }).then(res => {
      console.log(res);
      this.setData({
        today_merchants: res.data.data
      })
    })
    // 商家可提现收益
    app.request("/user/doPageStoreKtx", {
      is_shop: true
    }).then(res => {
      console.log(res);
      this.setData({
        can_cash: res.data.data
      })
    })
    // 商家销售明细
    app.request("/user/sales_detail_query", {
      is_shop: true
    }).then(res => {
      console.log(res);
      this.setData({
        sales_detail_query: res.data.data
      })
    })
  },
  get_catch() {
    wx.navigateTo({
      url: '/pages/get_cash/get_cash?type=1',
    })
  },
  cash_list() {
    wx.navigateTo({
      url: '/pages/brokerage_shop/brokerage_shop',
    })
  },
  //点击事件触发
  onChange(e) {
    var that = this
    let title = e.detail.title
    if (title == "已发货") {
      that.fetch()
    } else if (title == "待发货") {
      that.fetch()
    } else if (title == "销售明细") {
      that.fetch()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let shop_data = wx.getStorageSync('shop_data')
    this.setData({
      shop_data
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fetch()
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})